package aes.client;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.*;

import javax.swing.*;
import javax.swing.border.*;
import org.jfree.data.category.DefaultCategoryDataset;
import common.*;


public class ReportStudentInfoUI extends JPanel implements ActionListener, Observer{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2338196379551326685L;



	private ClientUI ui;
	private Client c;

	private JButton btnBack;
	private JComboBox cmbStudentList;
	private JTextField textMedian;
	private JTextField textAverage;
	private boolean loading;
	private JPanel panel;
	private BarChart plot;

	/**
	 * Constructor
	 * @param ui reference of parent frame
	 */ 
	public ReportStudentInfoUI(Client c,ClientUI ui) {
		super();
		this.ui = ui;
		this.c = c;

		JFrame frame = new JFrame();
		frame.setSize(640, 480);
		frame.getContentPane().add(this);

		// Handle messages from server.
		c.addObserver(this);

		setLayout(new GridBagLayout());

		panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		panel.setPreferredSize(new Dimension(600,400));

		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(5, 0, 5, 0);
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 0;
		add(panel,gbc_panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.rowHeights = new int[]{33, 33, 33, 33, 190, 33};
		gbl_panel.rowWeights = new double[]{1/7,1/7,1/7,1/7,1/7};

		gbl_panel.columnWidths = new int[]{100, 100, 100, 100, 100};
		gbl_panel.columnWeights = new double[]{1.0,1/6,1.0,1/6,1/6,};
		panel.setLayout(gbl_panel);

		JLabel lblTitle = new JLabel("Student Statistics:");
		lblTitle.setVerticalAlignment(SwingConstants.TOP);
		lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
		lblTitle.setBounds(7, 10, 70, 17);
		lblTitle.setFont(new Font("Tahoma", Font.BOLD, 14));

		GridBagConstraints gbc_lblTitle = new GridBagConstraints();
		gbc_lblTitle.gridwidth = 6;
		gbc_lblTitle.fill = GridBagConstraints.BOTH;
		gbc_lblTitle.insets = new Insets(5, 0, 5, 0);
		gbc_lblTitle.gridx = 0;
		gbc_lblTitle.gridy = 0;
		panel.add(lblTitle, gbc_lblTitle);	

		JLabel lblStudentId = new JLabel("Student ID:");
		GridBagConstraints gbc_lblStudentId = new GridBagConstraints();
		gbc_lblStudentId.anchor = GridBagConstraints.EAST;
		gbc_lblStudentId.insets = new Insets(0, 0, 5, 5);
		gbc_lblStudentId.gridx = 0;
		gbc_lblStudentId.gridy = 2;
		panel.add(lblStudentId, gbc_lblStudentId);

		cmbStudentList = new JComboBox();
		cmbStudentList.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				LoadMedianAndAvarage();
			}
		});
		GridBagConstraints gbc_cmbStudentList = new GridBagConstraints();
		gbc_cmbStudentList.insets = new Insets(0, 0, 5, 5);
		gbc_cmbStudentList.fill = GridBagConstraints.HORIZONTAL;
		gbc_cmbStudentList.gridx = 1;
		gbc_cmbStudentList.gridy = 2;
		panel.add(cmbStudentList, gbc_cmbStudentList);

		JLabel lblMedian = new JLabel("Median:");
		GridBagConstraints gbc_lblMedian = new GridBagConstraints();
		gbc_lblMedian.anchor = GridBagConstraints.EAST;
		gbc_lblMedian.insets = new Insets(0, 0, 5, 5);
		gbc_lblMedian.gridx = 0;
		gbc_lblMedian.gridy = 3;
		panel.add(lblMedian, gbc_lblMedian);

		textMedian = new JTextField();
		textMedian.setEditable(false);
		GridBagConstraints gbc_textMedian = new GridBagConstraints();
		gbc_textMedian.insets = new Insets(0, 0, 5, 5);
		gbc_textMedian.fill = GridBagConstraints.HORIZONTAL;
		gbc_textMedian.gridx = 1;
		gbc_textMedian.gridy = 3;
		panel.add(textMedian, gbc_textMedian);
		textMedian.setColumns(10);

		JLabel lblAverage = new JLabel("Average:");
		GridBagConstraints gbc_lblAverage = new GridBagConstraints();
		gbc_lblAverage.anchor = GridBagConstraints.EAST;
		gbc_lblAverage.insets = new Insets(0, 0, 5, 5);
		gbc_lblAverage.gridx = 2;
		gbc_lblAverage.gridy = 3;
		panel.add(lblAverage, gbc_lblAverage);

		textAverage = new JTextField();
		textAverage.setEditable(false);
		GridBagConstraints gbc_textAverage = new GridBagConstraints();
		gbc_textAverage.insets = new Insets(0, 0, 5, 5);
		gbc_textAverage.fill = GridBagConstraints.HORIZONTAL;
		gbc_textAverage.gridx = 3;
		gbc_textAverage.gridy = 3;
		panel.add(textAverage, gbc_textAverage);
		textAverage.setColumns(10);




		btnBack = new JButton("Back");
		btnBack.addActionListener(this) ;
		btnBack.setIcon(new ImageIcon(ReportStudentInfoUI.class.getResource("/aes/client/images/back.png")));
		GridBagConstraints gbc_btnBack = new GridBagConstraints();
		gbc_btnBack.anchor = GridBagConstraints.SOUTHWEST;
		gbc_btnBack.insets = new Insets(5, 5, 5, 5);
		gbc_btnBack.gridx = 0;
		gbc_btnBack.gridy = 5;
		panel.add(btnBack, gbc_btnBack);


		int[] ds={0,0,0,0,0,0,0,0,0};
		plot = new BarChart(ds,ui);
		GridBagConstraints gbc_plot = new GridBagConstraints();
		gbc_plot.gridwidth = 6;
		gbc_plot.gridheight = 1;
		gbc_plot.anchor = GridBagConstraints.WEST;
		gbc_plot.fill = GridBagConstraints.BOTH;
		gbc_plot.insets = new Insets(5, 5, 5, 5);
		gbc_plot.gridx = 0;
		gbc_plot.gridy = 4;

		panel.add(plot.getChartPanel(),gbc_plot);
		Initialize();
	}
	
	/**
	 * Initialize combo box
	 */
	private void Initialize() {
		// Get exam code
		loading = true;
		CommandMessage msg = new CommandMessage(CommandMessage.GET_STUDENTS, new Object[] {});
		try {
			c.sendToServer(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Load mean and average of student
	 */
	private void LoadMedianAndAvarage()
	{
		if (loading)
			return;
		// Get exams id's by subject and course.
		Student s = (Student)cmbStudentList.getSelectedItem();
		CommandMessage msg = new CommandMessage(CommandMessage.GET_STATISTICS_REPORT, new Object[] {"STUDENT", s.getId()});

		try {
			c.sendToServer(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Server message received event handler
	 */
	@Override
	public void update(Observable arg0, Object arg1) {
		if (arg1 instanceof CommandMessage) {
			CommandMessage ms=(CommandMessage)arg1;
			if (ms.getCommand().equals(CommandMessage.ACKNOWLEDGE_MESSAGE) && ((String)ms.getParams()[0]).equals(CommandMessage.GET_STUDENTS)) {
				// Fill students combobox.
				Student[] students = (Student[]) ms.getParams()[1];
				cmbStudentList.removeAllItems();
				for(int i=0;i<students.length;i++){
					cmbStudentList.addItem(students[i]);
				}

				loading = false;
				LoadMedianAndAvarage();
			}
			else if (ms.getCommand().equals(CommandMessage.ACKNOWLEDGE_MESSAGE) && ms.getParams().length == 3 && ((String)ms.getParams()[0]).equals(CommandMessage.GET_STATISTICS_REPORT) && ((String)ms.getParams()[1]).equals("STUDENT")) {
				// Show report.
				StatisticReportItem report = (StatisticReportItem)ms.getParams()[2];
				if (report != null) {
					textMedian.setText(Integer.toString(report.getMedian()));
					textAverage.setText(Double.toString(report.getAverage()));
					int[] ds = report.getDestribution();
					if (ds == null)
						ds = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0};
					DefaultCategoryDataset cd = (DefaultCategoryDataset)plot.getChartPanel().getChart().getCategoryPlot().getDataset();
					cd.setValue(ds[0], cd.getRowKey(0), cd.getColumnKey(0));
					cd.setValue(ds[1], cd.getRowKey(0), cd.getColumnKey(1));
					cd.setValue(ds[2], cd.getRowKey(0), cd.getColumnKey(2));
					cd.setValue(ds[3], cd.getRowKey(0), cd.getColumnKey(3));
					cd.setValue(ds[4], cd.getRowKey(0), cd.getColumnKey(4));
					cd.setValue(ds[5], cd.getRowKey(0), cd.getColumnKey(5));
					cd.setValue(ds[6], cd.getRowKey(0), cd.getColumnKey(6));
					cd.setValue(ds[7], cd.getRowKey(0), cd.getColumnKey(7));
					cd.setValue(ds[8], cd.getRowKey(0), cd.getColumnKey(8));
				}
			}
		}

	}

	/**
	 * Buttons click event handler
	 */
	@SuppressWarnings("static-access")
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(btnBack)) {
			// Back.
			ui.SetContent(ui.PRINCIPAL_REPORTS_SCREEN);
		}	
	}

}


